import {
    queryEnumList,
    queryFacilityGroup,
    querySpec,
    personDepartmentMapper,
    stordoc,
    queryAllFacilityDetail,
    queryOrganizationInfo,
    queryLessesByCondition,
    queryFacilityType,
} from '@/api/initData'


const getDefaultState = () => {
    return {
        enum: {}, //枚举
        facilityGroup: [],//设施组
        spec: [],//规格
        person: [],//人员
        stordoc: [],//仓库
        allFacility: [],//所有农业平台设施
        organizationInfo: [],//获取组织信息
        lessesList: [],//承租户
        facilityType: [],//设施类型
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    },
    SET_ENUM: (state, data) => {
        state.enum = data
    },
    SET_FISH_POND: (state, data) => {
        state.fishPond = data
    },
    SET_FACILITY_GROUP: (state, data) => {
        state.facilityGroup = data
    },
    SET_SPEC: (state, data) => {
        state.spec = data
    },
    SET_PERSON: (state, data) => {
        state.person = data
    },
    SET_STORDOC: (state, data) => {
        state.stordoc = data
    },
    SET_ALL_FACILITY: (state, data) => {
        state.allFacility = data
    },
    SET_ORGANIZATION_INFO: (state, data) => {
        state.organizationInfo = data
    },
    SET_LESSES: (state, data) => {
        state.lessesList = data
    },
    SET_FACILITY_TYPE: (state, data) => {
        state.facilityType = data
    },

}

const actions = {
    //枚举
    queryEnumList({ commit }) {
        console.log('枚举', '-----------------------')
        return new Promise((resolve, reject) => {
            queryEnumList({}).then(res => {
                const { data } = res
                commit('SET_ENUM', data || {});
                console.log('枚举', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //设施组
    queryFacilityGroup({ commit }) {
        return new Promise((resolve, reject) => {
            queryFacilityGroup({}).then(res => {
                const { data } = res
                commit('SET_FACILITY_GROUP', data || []);
                console.log('设施组', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //规格
    querySpec({ commit }) {
        return new Promise((resolve, reject) => {
            querySpec({}).then(res => {
                const { data } = res
                commit('SET_SPEC', data || []);
                console.log('规格', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //员工
    queryPerson({ commit }) {
        return new Promise((resolve, reject) => {
            personDepartmentMapper({}).then(res => {
                const { data } = res
                commit('SET_PERSON', data || []);
                console.log('员工', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //仓库
    queryStordoc({ commit }) {
        return new Promise((resolve, reject) => {
            stordoc({}).then(res => {
                const { data } = res
                commit('SET_STORDOC', data || []);
                console.log('仓库', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //查询所有农业平台设施
    queryAllFacilityDetail({ commit }) {
        return new Promise((resolve, reject) => {
            queryAllFacilityDetail({}).then(res => {
                const { data } = res;
                let list = data || [];
                list = list.filter(item => {
                    console.log(item.farmingTypeNo == 'sc')
                    return item.farmingTypeNo == 'sc'
                })
                commit('SET_ALL_FACILITY', list);
                console.log('所有农业平台设施', list)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //获取组织信息
    queryOrganizationInfo({ commit }) {
        return new Promise((resolve, reject) => {
            queryOrganizationInfo({}).then(res => {
                const { data } = res
                commit('SET_ORGANIZATION_INFO', data || []);
                console.log('组织信息', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //承租户信息
    queryLessesByCondition({ commit }) {
        return new Promise((resolve, reject) => {
            queryLessesByCondition({}).then(res => {
                const { data } = res
                commit('SET_LESSES', data || []);
                console.log('承租户信息', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
    //设施类型
    queryFacilityType({ commit }) {
        return new Promise((resolve, reject) => {
            queryFacilityType({}).then(res => {
                const { data } = res
                commit('SET_FACILITY_TYPE', data || []);
                console.log('设施类型', data)
            }).catch(error => {
                reject(error)
            })
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

